****************************************************************************************************************
**																											  **
** This do file is part of the replication material for the following article: 								  **
**  "Is There an Ideological Asymmetry in the Incumbency Effect? Evidence from U.S. Congressional Elections." **
** 		Authors: Morisi, Davide, Jost, John, Panagopoulos, Costas, and Jussi Valtonen		    			  **
** 		Journal: Social Psychological and Personality Science																	  **
**																											  **
** This file replicates the following step of the analysis: 												  **
**																											  **
**		RECODING CCES 2008 ORIGINAL DATASET			   													  	  **
**																											  **
****************************************************************************************************************

*Set working directory
cd "C:\Users\Davide Morisi\Dropbox\datasets\CCES"

*Open original dataset
*Stephen Ansolabehere, 2010, "CCES, Common Content, 2008", https://doi.org/10.7910/DVN/YUYIVB, Harvard Dataverse, V6, UNF:5:7eeaUMPVCcKDNxK6/kd37w== [fileUNF] 
*Downloaded on January 16, 2020 from https://dataverse.harvard.edu/dataset.xhtml?persistentId=hdl:1902.1/14003
use "2008\cces_2008_common.dta", clear

*********************
*covariates

*gender
fre V208
rename V208 gender
*age
sum V207
rename V207 birthyr
*education
fre V213
rename V213 educ
*race
fre V211
recode V211 4/8=99, gen(racer) // 99=other
*religion
fre V219
*1 protestant, 2 catholic, 3 other, 4 atheist, 5 agnostic, 6 none
recode V219 3/8=3 12=3 9=4 10=5 11=6, gen(religpewr) 
fre religpewr
*economy
fre CC302
rename CC302 economy
*state
fre V206
rename V206 state
clonevar state08 = state
*weight
sum V201
rename V201 weight1


*********************
*partisanship
*note: no profile question on PID, but pre-election questions

*party of respondent - pre-election
*3 categories
*1 democrat, 2 republican, 3 independent, 4 other, 5 dk
fre CC307 // Pre Election 3 point party ID - Generally speaking, do you think of yourself as a ...?
rename CC307 pid3

*7 categories
*1 strong dem, 2 not strong dem, 3 lean dem, 4 indep, 5 lean rep, 6 not strong rep, 7 strong rep, 8 dk 
fre CC307a // Pre Election 7 point Party ID
*Note: Coded from branching questions asked depending on response in CC307
rename CC307a pid7
fre pid3 pid7
ta pid7 pid3 // leaning partisans coded as independent


*party of respondent - POST ELECTION
*3 categories
fre CC423 // Post Election 3 point party ID
rename CC423 pid3post
*7 categories
fre CC424 // Post Election 7 point Party ID
*Note: Coded from branching questions asked depending on response in CC423
rename CC424 pid7post_2008
fre pid3post pid7post_2008
*1 dem, 2 lean dem, 3 indep, 4 lean rep, 5 rep
recode pid7post_2008 2=1 3=2 4=3 5=4 6=5 7=5 8=., gen(pid5post)



***
*ideology

*ideology PROFILE
*1 very lib, 2 lib, 3 moderate, 4 cons, 5 very cons, 6 dk
fre V243
rename V243 ideo5
fre ideo5

*ideology from PRE-ELECTION SURVEY
*The scales below represent the ideological spectrum from very liberal (0) to very conservative (100).
*The most centrist American is exactly at the middle (50).
fre CC317a // coded from 0 to 100
*hist CC317a, percent discrete
sum CC317a, d
ta CC317a if CC317a<50
*1 very lib, 2 lib, 3 somewhat lib, 4 mod, 5 somewhat con, 6 con, 7 very con
recode CC317a 0/10=1 11/39=2 40/49=3 50=4 51/60=5 61/89=6 90/100=7, gen(ideo7)
recode CC317a 0/10=1 11/35=2 36/45=3 46/54=4 55/64=5 65/89=6 90/100=7, gen(ideo7_08_alt1)
recode CC317a 0/17=1 18/34=2 35/49=3 50=4 51/65=5 66/82=6 83/100=7, gen(ideo7_08_alt2)
clonevar ideo_2008 = CC317a


***
*vote post
*voted?
fre CC403 // Which of the following statements best describes you?
rename CC403 turnout08
clonevar turnout = turnout08

*senate
*fre CC355 // CC355 Senate Vote Intent (In the race for U.S. Senator in your state, who do you prefer?)
fre CC411 // Senator vote - For whom did you vote for U.S. Senator?
rename CC411 votesen08
fre votesen08 // 1 dem cand, 2 rep cand, 3 other, 4 did not vote, 5 dk
clonevar vote_sen = votesen08

*house
fre CC412
rename CC412 votehou08
fre votehou08 // 1 dem cand, 2 rep cand, 3 other, 4 did not vote, 5 dk
clonevar vote_hou = votehou08


************************
*contextual var - SENATE

*incumbent candidates
des V660 V675
fre V660 V675
clonevar SenCand1Incumbent_postr = V660
clonevar SenCand2Incumbent_postr = V675
label de incumb 0"not incumbent" 1"incumbent", modify
label values SenCand1Incumbent_postr incumb
label values SenCand2Incumbent_postr incumb
fre SenCand1Incumbent_postr // senate dem candidate incumbent
fre SenCand2Incumbent_postr // senate rep candidate incumbent

*party of candidate
*1=dem, 2=rep, 3=other
fre V653  // dem
fre V655 // rep
encode V653, gen(V653r)
encode V655, gen(V655r)
gen SenCand1Party_postr = 1 if V653r!=. 
gen SenCand2Party_postr = 2 if V655r!=.
label de partycand 1"dem. candidate" 2"rep. candidate" 3"other candidate", modify
label val SenCand1Party_postr partycand
label val SenCand2Party_postr partycand 
fre SenCand1Party_postr // dem cand
fre SenCand2Party_postr // rep cand
ta SenCand1Party_postr SenCand2Party_postr

*republican vs dem incumbent
ta SenCand1Incumbent_postr SenCand2Incumbent_postr if (SenCand1Party_postr==1 & SenCand2Party_postr==2)
gen inc_rep_sen = 0 if (SenCand1Party_postr==1 & SenCand2Party_postr==2) & (SenCand1Incumbent_postr==1 & SenCand2Incumbent_postr==0)
replace inc_rep_sen = 1 if (SenCand1Party_postr==1 & SenCand2Party_postr==2) & (SenCand1Incumbent_postr==0 & SenCand2Incumbent_postr==1)
fre inc_rep_sen // 0= dem inc (rep not inc), 1= rep inc (dem not inc)
*including districts with no dem/rep incumbents
ta SenCand1Incumbent_postr SenCand2Incumbent_postr
gen inc_all_sen = inc_rep
recode inc_all_sen 0=-1
replace inc_all_sen = 0 if (SenCand1Party_postr==1 & SenCand2Party_postr==2) & (SenCand1Incumbent_postr==0 & SenCand2Incumbent_postr==0)
ta inc_all_sen // -1=dem inc, 0=inc not dem nor rep, 1=rep inc

*keep only states with elections, not open elections
ta state inc_rep_sen, nol
*filter
/*gen count2=1 if inc_rep!=.
egen count_sum2 = sum(count2), by(state)
gen filter_sen2=1 if inc_rep!=.
replace filter_sen2=2 if count_sum<10 & inc_rep!=.
replace filter_sen2=3 if (state==27 | state==37 | state==48) & inc_rep==0
replace filter_sen2=3 if (state==17 | state==25 | state==54) & inc_rep==1
ta state inc_rep if filter_sen2==1*/
bys state: ta V653 if inc_rep!=.
gen count=1 if inc_rep!=.
egen count_sum = sum(count), by(state V653)
gen filter_sen=1 if inc_rep!=.
replace filter_sen=2 if count_sum<10 & inc_rep!=.
ta state inc_rep_sen
ta state inc_rep_sen if filter_sen==1
clonevar filter_sen08 = filter_sen


**********************
*contextual var HOUSE

*incumbent candidates
des V622 V637
fre V622 // dem incumbent
fre V637 // rep incumbent
ta V618 V622 if state==1
ta V619 V637 if state==1
recode V622 1=1 2=0, gen(HouseCand1Incumbent_postr)
recode V637 1=1 2=0, gen(HouseCand2Incumbent_postr)
label val HouseCand1Incumbent_postr incumb
label val HouseCand2Incumbent_postr incumb
fre HouseCand1Incumbent_postr // dem inc
fre HouseCand2Incumbent_postr // rep inc

*party of candidate
*1=dem, 2=rep, 3=other
fre V618 // dem candidate
fre V619 // rep candidate
encode V618, gen(V618r)
encode V619, gen(V619r)
gen HouseCand1Party_postr = 1 if V618r!=.
gen HouseCand2Party_postr = 2 if V619r!=.
label val HouseCand1Party_postr partycand
label val HouseCand2Party_postr partycand
fre HouseCand1Party_postr // dem can
fre HouseCand2Party_postr // rep can
ta HouseCand1Party_postr HouseCand2Party_postr

*republican incumbent vs. dem incumbent
*note: with this var we restrict analysis to seats with dem vs. rep AND with inc. vs. not inc.
fre HouseCand1Party_postr HouseCand1Incumbent_postr
fre HouseCand2Party_postr HouseCand2Incumbent_postr
gen inc_rep_hou = 0 if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) ///
& (HouseCand1Incumbent_postr==1 & HouseCand2Incumbent_postr==0)
replace inc_rep_hou = 1 if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) ///
& (HouseCand1Incumbent_postr==0 & HouseCand2Incumbent_postr==1)
fre inc_rep_hou
ta HouseCand1Incumbent_postr HouseCand2Incumbent_postr if inc_rep_hou!=.
*including districts with no dem/rep incumbents
ta HouseCand1Incumbent_postr HouseCand2Incumbent_postr if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) 
gen inc_all_hou = inc_rep_hou
recode inc_all_hou 0=-1
replace inc_all_hou = 0 if (HouseCand1Party_postr==1 & HouseCand2Party_postr==2) & (HouseCand1Incumbent_postr==0 & HouseCand2Incumbent_postr==0)
fre inc_all_hou // -1=dem inc, 0=inc not dem nor rep, 1=rep inc

*keep only states with elections, not open elections
ta state inc_rep_hou
*filter
gen count1=1 if inc_rep_hou!=.
egen count_sum1 = sum(count1), by(state V618)
gen filter_hou=1 if inc_rep_hou!=.
replace filter_hou=2 if count_sum1<10 & inc_rep_hou!=.
ta state inc_rep_hou if filter_hou==1
clonevar filter_hou08 = filter_hou
*double check
des V618 V619
bys state: ta V618 if filter_hou==1 // e.g. wyoming missing because no incumbent
ta state if V618=="Gary S. Trauner" // seat without incumbent
ta state if V618=="Gary S. Trauner" & filter_hou==1 // removed when filter applied
ta state if V619=="Cynthia Lummis"


*******************
*combined variables > vote + incumbent status

*vote + incumbent status, all options - SENATE
fre turnout vote_sen
fre inc_all_sen // -1=dem inc, 0=inc not dem nor rep, 1=rep inc
ta vote_sen inc_all_sen
gen votinc_sen_all = 0 if turnout<5 | vote_sen==4 // did not vote
replace votinc_sen_all = 10 if vote_sen==1 & inc_all_sen==-1
replace votinc_sen_all = 11 if vote_sen==1 & inc_all_sen==0
replace votinc_sen_all = 12 if vote_sen==1 & inc_all_sen==1
replace votinc_sen_all = 20 if vote_sen==2 & inc_all_sen==-1
replace votinc_sen_all = 21 if vote_sen==2 & inc_all_sen==0
replace votinc_sen_all = 22 if vote_sen==2 & inc_all_sen==1
*label var votinc_sen_all "Voted for dem/rep by incumbent status - SENATE"
*label de votinc_sen_all 0"did not vote" 10"vote dem, dem inc"  11"vote dem, no inc"  12"vote dem, rep inc" ///
*20"vote rep, dem inc"  21"vote rep, no inc"  22"vote rep, rep inc" 
*label val votinc_sen_all votinc_sen_all
fre votinc_sen_all

*vote + incumbent status, all options - HOUSE
fre turnout vote_hou
fre inc_all_hou // -1=dem inc, 0=inc not dem nor rep, 1=rep inc
ta vote_hou inc_all_hou
gen votinc_hou_all = 0 if turnout<5 | vote_hou==4 // did not vote
replace votinc_hou_all = 10 if vote_hou==1 & inc_all_hou==-1
replace votinc_hou_all = 11 if vote_hou==1 & inc_all_hou==0
replace votinc_hou_all = 12 if vote_hou==1 & inc_all_hou==1
replace votinc_hou_all = 20 if vote_hou==2 & inc_all_hou==-1
replace votinc_hou_all = 21 if vote_hou==2 & inc_all_hou==0
replace votinc_hou_all = 22 if vote_hou==2 & inc_all_hou==1
*label var votinc_hou_all "Voted for dem/rep by incumbent status - HOUSE"
*label val votinc_hou_all votinc_sen_all
fre votinc_hou_all


******
*variables for merging datasets

gen year = 2008

rename V100 caseid08
clonevar caseid = caseid08

ta turnout, m
gen tookpost = 1 if turnout!=.

****
*create congressional districts
decode state, gen(stated)
drop if stated=="District of Columbia"
encode stated, gen(staten)
label var staten "States from 1 to 50 in alphabetical order"
fre staten // alphabetical order, from 0 to 50

*combine with district
rename V264 cd2008str
encode cd2008str, gen(cd2008)
*probl: states with one district, district coded as 00 instead of 01
*single district states: alaska, delaware, montana, north dakota, south dakota, vermont, wyoming
fre staten if cd2008==1
gen single_district = 1 if staten==2 | staten==8 | staten==26 | staten==34 | staten==41 | staten==45 | staten==50
fre staten if single_district==1

gen cd2008str_correct = cd2008str if single_district==.
replace cd2008str_correct="01" if single_district==1 & cd2008!=.
label var cd2008str_correct "Post  Congressional district 2008 00=01"
fre cd2008str cd2008str_correct if single_district==1

egen stcd=concat(staten cd2008str_correct)

*double check > all correct
fre stcd if staten==9 // florida
fre stcd if staten==9 & cd2008!=.
fre stcd if staten==32 // ny
fre stcd if staten==32 & cd2008!=.
fre stcd if staten==8 // delaware
fre stcd if staten==8 & cd2008!=.
fre stcd if staten==45 // vermont
fre stcd if staten==45 & cd2008!=.
fre stcd if staten==50 // wyoming
fre stcd if staten==50 & cd2008!=.

*
rename stcd stcd_string
gen stcd = real(stcd_string)
label var stcd_string "State (1 to 50 alphabetical) + congr. district (01 to n) STRING"
label var stcd "State (1 to 50 alphabetical) + congr. district (01 to n)"

***
/*merge with contextual var

save "CCES_2008_recoded2.dta", replace
import excel "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Other files\hr4618.xls", sheet("2008") firstrow clear
sum year stcd inc pwin dv dvp fr po1 po2 redist dexp rexp dpres switchb switcha
rename dv dv_orig
gen dv = real(dv_orig) // transform dv var into numeric
sum year stcd inc pwin dv dvp fr po1 po2 redist dexp rexp dpres switchb switcha
drop year dv_orig switchb switcha
save "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2008.dta", replace

use "CCES_2008_recoded2.dta", clear
*/

*merge info about previous election
merge m:1 stcd using "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2006.dta" // all matched from using
drop pwin dv dvp fr redist dexp rexp dpres _merge
rename inc inc_previous
rename po1 po1_previous
rename po2 po2_previous

*post election
merge m:1 stcd using "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2010.dta" // all matched
drop pwin dv dvp fr redist dexp rexp dpres _merge
rename inc inc_post
rename po1 po1_post
rename po2 po2_post

merge m:1 stcd using "C:\Users\Davide Morisi\Dropbox\research_inprogress\incumbency_effect\Replication do files\Data\house_data_2008.dta" // all matched from using
drop _merge



***
*voter registration status post
fre V256
recode V256 (2 3=0 "No/DK") (1=1 "Yes"), gen(register_post)
label var register_post "Registered to vote? (post)"
fre register_post

***
*job approval
*Please indicate whether you approve or disapprove of the job that each of the following are doing.
* 1 Strongly approve
* 2 somewhat Approve
* 3 somewaht Disapprove
* 4 Strongly disapprove
* 5 not sure

*House representative
fre CC335rep 
rename CC335rep approval_house

*senate
*2 senators for each state. 
fre CC335sen1 CC335sen2
rename CC335sen1 approval_sen1
rename CC335sen2 approval_sen2

***
*keep only selected variables
keep gender birthyr educ racer religpewr state state08 weight1 economy ///
pid3 pid7 pid3post pid5post pid7post_2008 ///
ideo5 ideo7 ideo7_08_alt1 ideo7_08_alt2 ideo_2008 ///
turnout turnout08 vote_sen votesen08 vote_hou votehou08 ///
SenCand1Incumbent_postr SenCand2Incumbent_postr SenCand1Party_postr SenCand2Party_postr ///
inc_rep_sen inc_all_sen filter_sen filter_sen08 ///
HouseCand1Incumbent_postr HouseCand2Incumbent_postr HouseCand1Party_postr HouseCand2Party_postr ///
inc_rep_hou inc_all_hou filter_hou filter_hou08 ///
votinc_sen_all votinc_hou_all ///
year caseid caseid08 tookpost ///
staten ///
cd2008 single_district cd2008str cd2008str_correct stcd_string stcd ///
inc pwin dvp fr po1 po2 redist dexp rexp dpres dv ///
register_post approval_house approval_sen1 approval_sen2 ///
inc_previous po1_previous po2_previous ///
inc_post po1_post po2_post

*save recoded file
save "CCES_2008_recoded2.dta", replace
